
package ${basepackage}.base;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ${basepackage}.common.Constants;

/**
 * 
 *
 */

public  class BaseAction extends Action {

    protected Log log = LogFactory.getLog(this.getClass());

    protected final static String SUCCESS = "success";
    protected final static String LIST = "list";
    protected final static String FORM = "form";
    protected final static String VIEW = "view";
    protected final static String EDIT = "edit";
    protected final static String ERROR = "error";
    protected final static String FAILURE = "failure";

    protected static final String ADD = "add";
    protected static final String MDF = "mdf";
    protected static final String DEL = "del";

    protected void path(HttpServletRequest request) {
		ServletContext ctx = request.getSession().getServletContext();
		if (ctx.getAttribute(Globals.CONTEXT_PATH) == null) {
			ctx.setAttribute(Globals.CONTEXT_PATH, request.getContextPath());
		}
	}


    /**
     * check session
     * @param request
     * @return true:valid  false:invalid
     */
    protected boolean sessionCheck(HttpServletRequest request){
    	String userId = (String)request.getSession().getAttribute(Constants.USERID);
		if (userId == null || userId.equals("")) {
			log.info("session invalid!");
			return false;
		}else{
			return true;
		}
    }

    protected void saveError(HttpServletRequest request, ActionMessage error) {
        ActionMessages errors = (ActionMessages) request
                .getAttribute(org.apache.struts.Globals.ERROR_KEY);
        if (errors == null) {
            errors = new ActionMessages();
        }
        errors.add(ActionMessages.GLOBAL_MESSAGE, error);
        this.saveErrors(request, errors);
    }

    protected void saveMessage(HttpServletRequest request, ActionMessage message) {
        ActionMessages messages = (ActionMessages) request
                .getAttribute(org.apache.struts.Globals.MESSAGE_KEY);
        if (messages == null) {
            messages = new ActionMessages();
        }
        messages.add(ActionMessages.GLOBAL_MESSAGE, message);
        this.saveErrors(request, messages);
    }
}
